﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Windows.Forms;
using FlickrNet;
using System.Collections.ObjectModel;
using InterOp;
using System.Collections.Generic;


/// <summary>
/// realize the connection to Flickr and imports data about a user
/// </summary>
public class FlickrConnection : ISocialNetwork
{
    //private string Frob;
    // Store the Frob in a private variable
    private string ApiKey = "5e1da9af2878963051f567be0035e929";
    private string SharedSecret = "dcb605194c0e3007";

    Flickr _flickrService;

    private Page m_WebPage;
    //private string username;

    public Page WebPage { get; set; }


    /// <summary>
    /// realize a connection to Flickr
    /// </summary>
    /// <param name="Page">current page used for retrieving information about the session</param>
    public FlickrConnection(Page Page)
	{
        m_WebPage = Page;
        // Create Flickr instance
        _flickrService = new Flickr(ApiKey, SharedSecret);
       
        // Get Frob	
        //Frob = _flickrService.AuthGetFrob();
        //string url = _flickrService.AuthCalcUrl(Frob, AuthLevel.Read);
	}

    #region ISocialNetwork Members


    /// <summary>
    /// import information about the profile of a Flickr user 
    /// </summary>
    /// <param name="username">username of the Flickr user whose information we want to retrieve </param>
    /// <returns>set of infomation about the Facebook user</returns>
    public UserData importProfile(string username)
    {
        Person p = _flickrService.PeopleGetInfo(_flickrService.PeopleFindByUsername(username).UserId);
        UserData data = new UserData();
        char[] delimiterChars = { ' ' };
        string[] names = p.RealName.Split(delimiterChars,2);
        data.Name = names[0];
        data.Surname = names[1];

        data.Nickname = p.UserName;
        data.Address = p.Location;
        return data;
      
            
    }


    /// <summary>
    /// import the friend list of a Flickr user 
    /// </summary>
    /// <param name="username">username of the Flickr user whose friends we want to retrieve </param>
    /// <returns>list of Ids of the Flickr friends of the user</returns>
    public List<String> importFriends(string username)
    {
        Contact[] contacts = _flickrService.ContactsGetPublicList(_flickrService.PeopleFindByUsername(username).UserId).ContactCollection;
        
        List<string> buddyList = new List<string>();
        foreach (Contact c in contacts)
        {
            buddyList.Add(c.UserId);
        }
        return buddyList;
    }

    #endregion

    #region IDisposable Members

    public void Dispose()
    {
       
    }

    #endregion
}


